package cn.wolfcode.crud.web.controller;


import cn.wolfcode.crud.domain.Customer;
import cn.wolfcode.crud.domain.Employee;
import cn.wolfcode.crud.domain.SystemDictionaryItem;
import cn.wolfcode.crud.qo.CustomerQuerObjcet;
import cn.wolfcode.crud.qo.JsonResult;
import cn.wolfcode.crud.service.ICustomerService;
import cn.wolfcode.crud.qo.QuerObjcet;
import cn.wolfcode.crud.service.IEmployeeService;
import cn.wolfcode.crud.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequiresPermissions(value = {"customer:list","潜在客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQuerObjcet qo){
        //只能够查看潜在客户
        qo.setStatus(Customer.STATUS_COMMON);
        //除了管理员和经理能够看到全部,其他就能够看自己的
        //拿到登陆对象
        Subject subject = SecurityUtils.getSubject();
        //判断角色
        if (!(subject.hasRole("ADMIN")||subject.hasRole("Market_Manager"))){
            Employee employee = (Employee)subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("PageResult", pageInfo);
        //看角色查询员工,还要去重
        List<Employee> employees=employeeService.selectByRole("Market","Market_Manager");
        model.addAttribute("employees", employees);
        //框框前查询专业
        List<SystemDictionaryItem> jobs=systemDictionaryItemService.selectItS("job");
        List<SystemDictionaryItem> sources=systemDictionaryItemService.selectItS("source");
        model.addAttribute("jobs", jobs);
        model.addAttribute("sources", sources);
        //联系方法下拉框,在谁那里方法招谁
        List<SystemDictionaryItem> ccts=systemDictionaryItemService.selectItS("communicationMethod");
        model.addAttribute("ccts", ccts);
        return "customer/potentialList";
    }
    //客户池
    @RequiresPermissions(value = {"customer:poolList","客户池页面"},logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQuerObjcet qo){
        //只看客户池的数据
        qo.setStatus(Customer.STATUS_POOL);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("PageResult", pageInfo);
        //拿到登陆对象
        Subject subject = SecurityUtils.getSubject();
        //判断角色
        if (!(subject.hasRole("ADMIN")||subject.hasRole("Market_Manager"))){
            Employee employee = (Employee)subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }
        //看角色查询员工,还要去重
        List<Employee> employees=employeeService.selectByRole("Market","Market_Manager");
        model.addAttribute("employees", employees);
        return "customer/poolList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户明细删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户明细新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){

        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }
    //年正号码
    @RequestMapping("/checkName")
    @ResponseBody
    //远程接收的一定是一对的才可以 valid:tuer
    public Map<String, Boolean> checkName(String tel) {

        Customer customer= customerService.checkName(tel);
        Map<String, Boolean> map =new HashMap<>();
        map.put("valid",customer==null);
        return map;

    }
}
